package yanjunwei.rbac.service.collection;

import yanjunwei.rbac.domain.dto.RoleDto;
import yanjunwei.rbac.domain.entity.Role;
import yanjunwei.rbac.domain.vo.Result;
import yanjunwei.rbac.domain.vo.RoleVo;
import yanjunwei.rbac.service.IRoleService;
import yanjunwei.rbac.service.IUserService;

import yanjunwei.rbac.util.SingletonFactory;

import java.time.LocalDateTime;
@Deprecated

public class RoleService extends BaseService<Role,RoleDto,RoleVo> implements IRoleService {



    private IUserService userService;


    private RoleService(){

temp=new RoleVo[0];
    }



@Override
RoleVo convertToVo(Role role){
    return new RoleVo(role.getId(),role.createAt,role.updateAt,role.name,role.dataScope);
    }
@Override
     Role createEntity(RoleDto dto,int id){
    LocalDateTime now=LocalDateTime.now();
        return new Role(id,now,now,dto.name,dto.dataScope);
    }
    @Override
    void updateEntity(RoleDto dto,Role role){
        role.updateAt=LocalDateTime.now();
        role.name=dto.name;
        role.dataScope=dto.dataScope;
    }

@Override
public Result delete(int id){
        if (!existsById(id)){
        return Result.failure("删除失败，不存在该数据");}

        if (userService==null){
            userService= SingletonFactory.getInstance(UserService.class);
        }

        if (userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data.remove(id);
        return Result.success("删除成功",null);
    }
}
